import { SelectedItem } from "@/components/tree-select/schema";

export function convertStringToTreeSelected(str: string): SelectedItem {
  if (str == "") {
    return {};
  }
  // 分割字符串为数组
  const arr = str.split(",");
  // 初始化结果对象
  const result: SelectedItem = {};
  // 遍历数组，构建对象
  arr.forEach((item) => {
    // 将字符串转为数字作为键
    const key = Number(item);
    result[key] = true;
  });
  return result;
}

export function convertTreeSelectedToString(obj: SelectedItem): string {
  const keys = Object.keys(obj);

  // 拼接为逗号分隔的字符串
  return keys.join(",");
}
